package com.fengniu.ejb.dao.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.fengniu.ejb.dao.ContentDaoLocal;
import com.fengniu.ejb.dao.ContentDaoRemote;
import com.fengniu.ejb.dao.util.DaoUtil;
import com.fengniu.ejb.dao.util.IEJBConstants;
import com.fengniu.ejb.entity.Content;
import com.fengniu.ejb.entity.Content2Category;
import com.fengniu.ejb.entity.Content2Content;
import com.fengniu.ejb.entity.User;

@Stateless
@LocalBinding(jndiBinding = IEJBConstants.ContentDaoLocalJndiName)
@RemoteBinding(jndiBinding = IEJBConstants.ContentDaoRemoteJndiName)
public class ContentDaoImpl implements ContentDaoLocal, ContentDaoRemote {
	static Logger logger = Logger.getLogger(ContentDaoImpl.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = -7861568656574099969L;

	@PersistenceContext(unitName = "default")
	protected EntityManager em;

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public Content saveContent(Content content) {
		Calendar now = Calendar.getInstance();
		content.setLastUpdatedDate(now.getTime());
		Content c = em.merge(content);
		return c;
	}

	public Content findContentByContentId(String contentId) {
		List contentList = this.findByProperty("contentId", contentId, 1);
		if (contentList != null && contentList.size() > 0)
			return (Content) contentList.get(0);
		return null;
	}

	// public Content2Category findContent2CategoryBy(String contentId,
	// Content.Type[] contentTypes,
	// Content2Category.RelationType[] relationTypes, String id,
	// Content2Category.Status status) {
	// Content2Category result = null;
	//
	// List<Content2Category> c2gs = this.getListContent2CategoryBy(contentId,
	// relationTypes, id, status,
	// contentTypes, 0, 1, null);
	// if (c2gs != null && c2gs.size()>0)
	// result = c2gs.get(0);
	// return result;
	// }
	//
	// public Content2Content findContent2ContentBy(String contentId,
	// Content2Content.RelationType[] relationTypes,
	// String contentId2, Content2Content.Status status) {
	// Content2Content result = null;
	// List<Content2Content> c2cs = this.getListContent2ContentBy(contentId,
	// relationTypes, status, contentId2, 0, 1,
	// null);
	// if (c2cs != null && c2cs.size()>0)
	// result = c2cs.get(0);
	// return result;
	// }

	public void setEm(EntityManager em) {
		this.em = em;
	}

	public Content findContentBySysId(long sysId) {
		return em.find(Content.class, sysId);
	}

	public Content2Category saveContent2Category(Content2Category content2Category) {
		Calendar now = Calendar.getInstance();
		content2Category.setLastUpdatedDate(now.getTime());
		Content2Category c = em.merge(content2Category);
		return c;
	}

	public Content2Content saveContent2Content(Content2Content content2Content) {
		Calendar now = Calendar.getInstance();
		content2Content.setLastUpdatedDate(now.getTime());
		Content2Content c = em.merge(content2Content);
		return c;
	}

	// public Content2Category cancelContent2Category(long
	// content2CategorySysId) {
	// Content2Category result = null;
	// Content2Category u2u = em.find(Content2Category.class,
	// content2CategorySysId);
	// if (u2u != null) {
	// u2u.setStatus(Content2Category.Status.cancell.getCode());
	// result = em.merge(u2u);
	// }
	// return result;
	// }

	// public Content2Content cancelContent2Content(long content2ContentSysId) {
	// Content2Content result = null;
	// Query q = em.createQuery(
	// "update Content2Content c2c set c2c.status=:status where
	// c2c.sysId=:content2ContentSysId")
	// .setParameter("status",
	// Content2Content.Status.cancell.getCode()).setParameter("content2ContentSysId",
	// content2ContentSysId);
	// q.executeUpdate();
	// // Content2Content u2u = em.find(Content2Content.class,
	// // content2ContentSysId);
	// // if (u2u != null) {
	// // u2u.setStatus(Content2Content.Status.cancell.getCode());
	// // result = em.merge(u2u);
	// // }
	// return result;
	// }

	// public long getCountContent2ContentBy(long contentSysId,
	// Content2Content.RelationType[] relationTypes,
	// Content2Content.Status status, long contentSysId2) {
	// Query q = this.getQueryContent2ContentBy(contentSysId, relationTypes,
	// status, contentSysId2, -1, -1, null,
	// false);
	//
	// Long result = (Long) q.getSingleResult();
	//
	// return result;
	// }
	//
	// public List<Content2Content> getListContent2ContentBy(long contentSysId,
	// Content2Content.RelationType[] relationTypes, Content2Content.Status
	// status, long contentSysId2, int pgno,
	// int pgsize, String orderBy) {
	// Query q = this.getQueryContent2ContentBy(contentSysId, relationTypes,
	// status, contentSysId2, pgno, pgsize,
	// orderBy, true);
	// List result = DaoUtil.getResultList(q, pgno, pgsize);
	// return result;
	// }
	//
	// private Query getQueryContent2ContentBy(long contentSysId,
	// Content2Content.RelationType[] relationTypes,
	// Content2Content.Status status, long contentSysId2, int pgno, int pgsize,
	// String orderBy, boolean isGetList) {
	// String ent = isGetList ? "distinct c2c" : "count(c2c)";
	// String eql = "select " + ent + " from Content2Content c2c";
	// String whereand = "";
	// if (contentSysId > 0) {
	// whereand = DaoUtil.getWhereOrAndStr(whereand);
	// eql += whereand + " c2c.content.sysId=:contentSysId ";
	// }
	// if (!DaoUtil.isNull(relationTypes)) {
	// whereand = DaoUtil.getWhereOrAndStr(whereand);
	// eql += whereand + " c2c.relationType IN (:relationTypes) ";
	// }
	// if (status != null) {
	// whereand = DaoUtil.getWhereOrAndStr(whereand);
	// eql += whereand + " c2c.status=:status ";
	// }
	// if (contentSysId2 > 0) {
	// whereand = DaoUtil.getWhereOrAndStr(whereand);
	// eql += whereand + " c2c.content2.sysId=:contentSysId2 ";
	// }
	// Query q = em.createQuery(eql);
	// if (contentSysId > 0) {
	// q.setParameter("contentSysId", contentSysId);
	// }
	// if (!DaoUtil.isNull(relationTypes)) {
	// List rts = new ArrayList(0);
	// for (Content2Content.RelationType e : relationTypes) {
	// rts.add(e.getCode());
	// }
	// q.setParameter("relationTypes", rts);
	// }
	// if (status != null) {
	// q.setParameter("status", status.getCode());
	// }
	// if (contentSysId2 > 0) {
	// q.setParameter("contentSysId2", contentSysId2);
	// }
	//
	// return q;
	// }
	// private Query getQueryContent2ContentBy(String contentId,
	// Content2Content.RelationType[] relationTypes,
	// Content2Content.Status status, String contentId2, int pgno, int pgsize,
	// String orderBy, boolean isGetList) {
	// String ent = isGetList ? "distinct c2c" : "count(c2c)";
	// String eql = "select " + ent + " from Content2Content c2c";
	// String whereand = "";
	// if (contentId !=null) {
	// whereand = DaoUtil.getWhereOrAndStr(whereand);
	// eql += whereand + " c2c.content.contentId=:contentId ";
	// }
	// if (!DaoUtil.isNull(relationTypes)) {
	// whereand = DaoUtil.getWhereOrAndStr(whereand);
	// eql += whereand + " c2c.relationType IN (:relationTypes) ";
	// }
	// if (status != null) {
	// whereand = DaoUtil.getWhereOrAndStr(whereand);
	// eql += whereand + " c2c.status=:status ";
	// }
	// if (contentId2 !=null) {
	// whereand = DaoUtil.getWhereOrAndStr(whereand);
	// eql += whereand + " c2c.content2.contentId=:contentId2 ";
	// }
	// Query q = em.createQuery(eql);
	// if (contentId !=null) {
	// q.setParameter("contentId", contentId);
	// }
	// if (!DaoUtil.isNull(relationTypes)) {
	// List rts = new ArrayList(0);
	// for (Content2Content.RelationType e : relationTypes) {
	// rts.add(e.getCode());
	// }
	// q.setParameter("relationTypes", rts);
	// }
	// if (status != null) {
	// q.setParameter("status", status.getCode());
	// }
	// if (contentId2 !=null) {
	// q.setParameter("contentId2", contentId2);
	// }
	//
	// return q;
	// }

	public Content2Content findContent2ContentBy(long content2ContentSysId) {
		return em.find(Content2Content.class, content2ContentSysId);
	}

	// private Query getQueryContentBy(long editorSysId, Content.Type[]
	// contentTypes, Content.Status status, int pgno,
	// int pgsize, String orderBy, boolean isGetList) {
	// String ent = isGetList ? "distinct w" : "count(w)";
	//
	// String eql = "select " + ent + " from Content w ";
	// String whereand = "";
	// if (editorSysId > -1) {
	// whereand = DaoUtil.getWhereOrAndStr(whereand);
	// eql += whereand + " w.editor.sysId=:editorSysId ";
	// }
	// if (!DaoUtil.isNull(contentTypes)) {
	// whereand = DaoUtil.getWhereOrAndStr(whereand);
	// eql += whereand + " w.type IN (:contentTypes) ";
	// }
	// if (status != null) {
	// whereand = DaoUtil.getWhereOrAndStr(whereand);
	// eql += whereand + " w.status=:status ";
	// }
	//
	// Query q = em.createQuery(eql);
	// if (editorSysId > -1) {
	// q.setParameter("editorSysId", editorSysId);
	// }
	// if (!DaoUtil.isNull(contentTypes)) {
	// ArrayList<String> rts = new ArrayList<String>(0);
	// for (int i = 0; i < contentTypes.length; i++) {
	// rts.add(contentTypes[i].getCode());
	// }
	// q.setParameter("contentTypes", rts);
	// }
	// if (status != null) {
	// q.setParameter("status", status);
	// }
	// return q;
	// }
	//
	// public long getCountContentBy(long editorSysId, Content.Type[]
	// contentTypes, Content.Status status) {
	// Long result = new Long(0);
	// Query q = this.getQueryContentBy(editorSysId, contentTypes, status, -1,
	// -1, null, false);
	// result = (Long) q.getSingleResult();
	// return result;
	// }
	//
	// public List<Content> getListContentBy(long editorSysId, Content.Type[]
	// contentTypes, Content.Status status,
	// int pgno, int pgsize, String orderBy) {
	// Query q = this.getQueryContentBy(editorSysId, contentTypes, status, pgno,
	// pgsize, orderBy, true);
	// List result = DaoUtil.getResultList(q, pgno, pgsize);
	// return result;
	// }
	//
	//
	// private Query getQueryContent2CategoryBy(long contentSysId,
	// Content2Category.RelationType[] relationTypes,
	// long categorySysId, Content2Category.Status status, Content.Type[]
	// contentTypes, int pgno, int pgsize,
	// String orderBy, boolean isGetList) {
	//
	// String ent = isGetList ? "distinct c2g" : "count(c2g)";
	//
	// String eql = "select " + ent + " from Content2Category c2g";
	// String whereand = "";
	// if (contentSysId > 0) {
	// whereand = DaoUtil.getWhereOrAndStr(whereand);
	// eql += whereand + " c2g.content.sysId=:contentSysId ";
	// }
	// if (!DaoUtil.isNull(relationTypes)) {
	// whereand = DaoUtil.getWhereOrAndStr(whereand);
	// eql += whereand + " c2g.relationType IN (:relationTypes) ";
	// }
	// if (categorySysId > 0) {
	// whereand = DaoUtil.getWhereOrAndStr(whereand);
	// eql += whereand + " c2g.category.sysId=:categorySysId ";
	// }
	// if (status != null) {
	// whereand = DaoUtil.getWhereOrAndStr(whereand);
	// eql += whereand + " c2g.status=:status ";
	// }
	// if (!DaoUtil.isNull(contentTypes)) {
	// whereand = DaoUtil.getWhereOrAndStr(whereand);
	// eql += whereand + " c2g.content.type IN (:contentTypes) ";
	// }
	// Query q = em.createQuery(eql);
	// if (contentSysId > 0) {
	// q.setParameter("contentSysId", contentSysId);
	// }
	// if (!DaoUtil.isNull(relationTypes)) {
	// ArrayList<String> rts = new ArrayList<String>(0);
	// for (int i = 0; i < relationTypes.length; i++) {
	// rts.add(relationTypes[i].getCode());
	// }
	// q.setParameter("relationTypes", rts);
	// }
	// if (categorySysId > 0) {
	// q.setParameter("categorySysId", categorySysId);
	// }
	// if (status != null) {
	// q.setParameter("status", status.getCode());
	// }
	// if (!DaoUtil.isNull(contentTypes)) {
	// ArrayList<String> rts = new ArrayList<String>(0);
	// for (int i = 0; i < contentTypes.length; i++) {
	// rts.add(contentTypes[i].getCode());
	// }
	// q.setParameter("contentTypes", rts);
	// }
	//
	// return q;
	// }
	//

	private Query getQueryContent2CategoryBy(long contentSysId, Integer[] relationIds, long categorySysId,
			Integer[] statusIds, Integer[] contentTypeIds, int pgno, int pgsize, String orderBy, boolean isGetList) {

		String ent = isGetList ? "c2g" : "count(c2g)";

		String eql = "select " + ent + " from  Content2Category c2g";
		String whereand = "";
		if (contentSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.content.sysId=:contentSysId ";
		}
		if (DaoUtil.isNull(relationIds) == false) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.relationId IN (:relationIds) ";
		}
		if (categorySysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.category.sysId=:categorySysId ";
		}
		if (DaoUtil.isNull(statusIds) == false) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.status IN (:statusIds) ";
		}
		if (DaoUtil.isNull(contentTypeIds) == false) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.content.typeId IN (:contentTypeIds) ";
		}
		if (isGetList && orderBy != null && orderBy.trim() != "") {
			eql += " ORDER BY " + orderBy;
		}
		logger.debug("eql=[" + eql + "]");

		Query q = em.createQuery(eql);
		if (contentSysId > -1) {
			q.setParameter("contentSysId", contentSysId);
		}
		if (DaoUtil.isNull(relationIds) == false) {
			List rts = DaoUtil.getListFromArray(relationIds);
			q.setParameter("relationIds", rts);
		}
		if (categorySysId > -1) {
			q.setParameter("categorySysId", categorySysId);
		}
		if (DaoUtil.isNull(statusIds) == false) {
			List rts = DaoUtil.getListFromArray(statusIds);
			q.setParameter("statusIds", rts);
		}
		if (DaoUtil.isNull(contentTypeIds) == false) {
			List rts = DaoUtil.getListFromArray(contentTypeIds);
			q.setParameter("contentTypeIds", rts);
		}

		return q;
	}

	private Query getQueryContent2CategoryWithScalrBy(String contentId, Integer[] relationIds, String categoryId,
			String brandCategoryId, String[] statusIds, String[] contentTypeIds, int pgno, int pgsize, String orderBy,
			boolean isGetList) {

		String ent = isGetList ? "c2g" : "count(c2g)";

		String eql = "select c2g.content,c2g.category,count(c2g.content.contentId) from  Content2Category c2g";
		String whereand = "";
		if (brandCategoryId != null) {
			eql += ", Content2Category c2gbrand WHERE c2g.content = c2gbrand.content AND c2gbrand.category.categoryId=:brandCategoryId";
			whereand = DaoUtil.getWhereOrAndStr(whereand);
		}
		if (contentId != null) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.content.contentId=:contentId ";
		}
		if (DaoUtil.isNull(relationIds) == false) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.relationId IN (:relationIds) ";
		}
		if (categoryId != null) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.category.categoryId=:categoryId ";
		}
		if (DaoUtil.isNull(statusIds) == false) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.status IN (:statusIds) ";
		}
		if (DaoUtil.isNull(contentTypeIds) == false) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.content.typeId IN (:contentTypeIds) ";
		}
		logger.info("eql=[" + eql + "]");

		Query q = em.createQuery(eql);
		if (brandCategoryId != null) {
			q.setParameter("brandCategoryId", brandCategoryId);
		}
		if (contentId != null) {
			q.setParameter("contentId", contentId);
		}
		if (DaoUtil.isNull(relationIds) == false) {
			List rts = DaoUtil.getListFromArray(relationIds);
			q.setParameter("relationIds", rts);
		}
		if (categoryId != null) {
			q.setParameter("categoryId", categoryId);
		}
		if (DaoUtil.isNull(statusIds) == false) {
			List rts = DaoUtil.getListFromArray(statusIds);
			q.setParameter("statusIds", rts);
		}
		if (DaoUtil.isNull(contentTypeIds) == false) {
			List rts = DaoUtil.getListFromArray(contentTypeIds);
			q.setParameter("contentTypeIds", rts);
		}

		return q;
	}

	private Query getQueryContent2CategoryBy(String contentId, Integer[] relationIds, String categoryId,
			String brandCategoryId, Integer[] statusIds, Integer[] contentTypeIds, int pgno, int pgsize,
			String orderBy, boolean isGetList) {

		String ent = isGetList ? "c2g" : "count(c2g)";

		String eql = "select " + ent + " from  Content2Category c2g";
		String whereand = "";
		if (brandCategoryId != null) {
			eql += ", Content2Category c2gbrand WHERE c2g.content = c2gbrand.content AND c2gbrand.category.categoryId=:brandCategoryId";
			whereand = DaoUtil.getWhereOrAndStr(whereand);
		}
		if (contentId != null) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.content.contentId=:contentId ";
		}
		if (DaoUtil.isNull(relationIds) == false) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.relationId IN (:relationIds) ";
		}
		if (categoryId != null) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.category.categoryId=:categoryId ";
		}
		if (DaoUtil.isNull(statusIds) == false) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.status IN (:statusIds) ";
		}
		if (DaoUtil.isNull(contentTypeIds) == false) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.content.typeId IN (:contentTypeIds) ";
		}
		if (isGetList && orderBy != null && orderBy.trim() != "") {
			eql += " ORDER BY " + orderBy;
		}
		logger.info("eql=[" + eql + "]");

		Query q = em.createQuery(eql);
		if (brandCategoryId != null) {
			q.setParameter("brandCategoryId", brandCategoryId);
		}
		if (contentId != null) {
			q.setParameter("contentId", contentId);
		}
		if (DaoUtil.isNull(relationIds) == false) {
			List rts = DaoUtil.getListFromArray(relationIds);
			q.setParameter("relationIds", rts);
		}
		if (categoryId != null) {
			q.setParameter("categoryId", categoryId);
		}
		if (DaoUtil.isNull(statusIds) == false) {
			List rts = DaoUtil.getListFromArray(statusIds);
			q.setParameter("statusIds", rts);
		}
		if (DaoUtil.isNull(contentTypeIds) == false) {
			List rts = DaoUtil.getListFromArray(contentTypeIds);
			q.setParameter("contentTypeIds", rts);
		}

		return q;
	}

	private Query getQueryContent2ContentBy(String contentId, Integer[] relationIds, String contentId2,
			Integer[] statusIds, String orderBy, boolean isGetList) {

		String ent = isGetList ? "c2c" : "count(c2c)";

		String eql = "select " + ent + " from  Content2Content c2c";
		String whereand = "";
		if (contentId != null) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2c.content.contentId=:contentId ";
		}
		if (DaoUtil.isNull(relationIds) == false) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2c.relationId IN (:relationIds) ";
		}
		if (contentId2 != null) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2c.content2.contentId=:contentId2 ";
		}
		if (DaoUtil.isNull(statusIds) == false) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2c.status IN (:statusIds) ";
		}
		if (isGetList && orderBy != null && orderBy.trim() != "") {
			eql += " ORDER BY " + orderBy;
		}
		logger.info("eql=[" + eql + "]");

		Query q = em.createQuery(eql);
		if (contentId != null) {
			q.setParameter("contentId", contentId);
		}
		if (DaoUtil.isNull(relationIds) == false) {
			List rts = DaoUtil.getListFromArray(relationIds);
			q.setParameter("relationIds", rts);
		}
		if (contentId2 != null) {
			q.setParameter("contentId2", contentId2);
		}
		if (DaoUtil.isNull(statusIds) == false) {
			List rts = DaoUtil.getListFromArray(statusIds);
			q.setParameter("statusIds", rts);
		}

		return q;
	}

	public Content2Category findContent2CategoryBy(String contentId, Integer relationId, String categoryId) {
		Content2Category result = null;
		Integer[] rIds = { relationId };
		List<Content2Category> c2cs = this.getListContent2CategoryBy(contentId, rIds, categoryId, null, null, null, 0,
				1, null);
		if (c2cs != null && c2cs.size() > 0)
			result = c2cs.get(0);
		return result;
	}

	public Long getCountContent2CategoryBy(String contentId, Integer[] relationIds, String categoryId,
			String brandCategoryId, Integer[] statusIds, Integer[] contentTypeIds) {
		Long result = new Long(0);
		Query q = this.getQueryContent2CategoryBy(contentId, relationIds, categoryId, brandCategoryId, statusIds,
				contentTypeIds, -1, -1, null, false).setHint("org.hibernate.cacheable", new Boolean(true));//.setHint("org.hibernate.cacheRegion", "/entity/Content2Category/Count")
		;

		result = (Long) q.getSingleResult();

		return result;
	}

	public long getCountContent2ContentBy(long contentSysId, String[] relationIds, String[] statusIds,
			long contentSysId2) {
		// TODO Auto-generated method stub
		return 0;
	} 

	public long getCountContent2ContentBy(String contentId, Integer[] relationIds, Integer[] statusIds,
			String contentId2) {
		Long result = new Long(0);
		Query q = this.getQueryContent2ContentBy(contentId, relationIds, contentId2, statusIds, null, false);

		result = (Long) q.getSingleResult();

		return result;
	}

	public List<Content2Category> getListContent2CategoryBy(long contentSysId, Integer[] relationIds,
			long categorySysId, Integer[] statusIds, Integer[] contentTypeIds, int pgno, int pgsize, String orderBy) {
		Query q = this.getQueryContent2CategoryBy(contentSysId, relationIds, categorySysId, statusIds, contentTypeIds,
				-1, -1, orderBy, true).setHint("org.hibernate.cacheable", new Boolean(true));
		//.setHint(				"org.hibernate.cacheRegion", "/entity/Content2Category/List")
		List result = DaoUtil.getResultList(q, pgno, pgsize);
		return result;
	}

	public long getCountContent2CategoryBy(long contentSysId, Integer[] relationIds, long categorySysId,
			Integer[] statusIds, Integer[] contentTypeIds) {
		Long result = new Long(0);
		Query q = this.getQueryContent2CategoryBy(contentSysId, relationIds, categorySysId, statusIds, contentTypeIds,
				-1, -1, null, false);

		result = (Long) q.getSingleResult();

		return result;
	}

	public List<Content2Category> getListContent2CategoryBy(String contentId, Integer[] relationIds, String categoryId,
			String brandCategoryId, Integer[] statusIds, Integer[] contentTypeIds, int pgno, int pgsize, String orderBy) {
		Query q = this.getQueryContent2CategoryBy(contentId, relationIds, categoryId, brandCategoryId, statusIds,
				contentTypeIds, -1, -1, orderBy, true).setHint("org.hibernate.cacheable", new Boolean(true));//.setHint("org.hibernate.cacheRegion", "/entity/Content2Category/List")
		List result = DaoUtil.getResultList(q, pgno, pgsize);
		return result;
	}

	public List<Content2Category> getListContent2CategoryWithScalarBy(String contentId, Integer[] relationIds,
			String categoryId, String brandCategoryId, String[] statusIds, String[] contentTypeIds, int pgno,
			int pgsize, String orderBy) {
		// TODO can not work with scalar
		Query q = this.getQueryContent2CategoryWithScalrBy(contentId, relationIds, categoryId, brandCategoryId,
				statusIds, contentTypeIds, -1, -1, null, true);
		List result = DaoUtil.getResultList(q, pgno, pgsize);
		return result;
	}

	public List<Content2Content> getListContent2ContentBy(long contentSysId, String[] relationIds, String[] statusIds,
			long contentSysId2, int pgno, int pgsize, String orderBy) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Content2Content> getListContent2ContentBy(String contentId, Integer[] relationIds, Integer[] statusIds,
			String contentId2, int pgno, int pgsize, String orderBy) {
		Query q = this.getQueryContent2ContentBy(contentId, relationIds, contentId2, statusIds, orderBy, true);
		List result = DaoUtil.getResultList(q, pgno, pgsize);
		return result;
	}

	public void deleteContent2Category(long content2CategorySysId) {
		Content2Category c2g = em.find(Content2Category.class, content2CategorySysId);
		if (c2g != null)
			em.remove(c2g);
	}

	public void deleteContentByContentId(String contentId) {
		Content u = findContentByContentId(contentId);
		if (u != null)
			em.remove(u);
	}

	// --------------------------------------
	@SuppressWarnings("unchecked")
	private List<User> findByProperty(String propertyName, Object value, int size) {
		String queryString = "select model from Content model where model." + propertyName + "= :propertyValue";
		return em.createQuery(queryString).setParameter("propertyValue", value).setFirstResult(0).setMaxResults(size)
				.getResultList();
	}

	public long getCountContentBy(Integer[] contentTypeIds, String[] statusIds, Date beforeLastUpdatedDate) {
		Long result = new Long(0);
		Query q = this.getQueryContentBy(contentTypeIds, statusIds, beforeLastUpdatedDate, null, false);

		result = (Long) q.getSingleResult();
		return result;
	}

	public List<Content> getListContentBy(Integer[] contentTypeIds, String[] statusIds, Date beforeLastUpdatedDate,
			int pgno, int pgsize, String orderBy) {
		Query q = this.getQueryContentBy(contentTypeIds, statusIds, beforeLastUpdatedDate, orderBy, true);
		List result = DaoUtil.getResultList(q, pgno, pgsize);
		return result;
	}

	private Query getQueryContentBy(Integer[] contentTypeIds, String[] statusIds, Date beforeLastUpdatedDate,
			String orderBy, boolean isGetList) {

		String ent = isGetList ? " c " : "count(c)";

		String eql = "select " + ent + " from  Content c";
		String whereand = "";

		if (!DaoUtil.isNull(contentTypeIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c.typeId IN (:contentTypeIds) ";
		}
		if (isGetList && orderBy != null && orderBy.trim() != "") {
			eql += " ORDER BY " + orderBy;
		}
		logger.debug("eql=[" + eql + "]");

		Query q = em.createQuery(eql);

		if (DaoUtil.isNull(contentTypeIds) == false) {
			List rts = DaoUtil.getListFromArray(contentTypeIds);
			q.setParameter("contentTypeIds", rts);
		}

		return q;
	}

	@SuppressWarnings("unchecked")
	public Content2Category findContentLeafCategoryBy(long contentSysId, Integer relationId, Integer categoryTypeID) {
		if (contentSysId > -1 && relationId != null) {
			Query q = em.createNamedQuery("Content.eql.findContentLeafCategoryBy");
			q.setParameter("contentSysId", contentSysId).setParameter("relationId", relationId).setParameter(
					"categoryTypeID", categoryTypeID);
			List<Content2Category> result = DaoUtil.getResultList(q, 0, 1);
			if (result != null && result.size() > 0) {
				return result.get(0);
			}
		}
		return null;
	}

	public Content2Category findContentLeafCategoryBy(long contentSysId, Integer relationId) {
		if (contentSysId > -1 && relationId != null) {
			String eql = "SELECT c2g FROM  Content2Category  c2g where  c2g.content.sysId=:contentSysId AND c2g.relationId=:relationId order by c2g.category.sysId DESC";
			Query q = em.createQuery(eql);
			q.setParameter("contentSysId", contentSysId).setParameter("relationId", relationId);
			List<Content2Category> result = DaoUtil.getResultList(q, 0, 1);
			if (result != null && result.size() > 0) {
				return result.get(0);
			}
		}
		return null;
	}

}
